﻿Public Class frmLedgerSumAccount

    Private m_periodIndex As Integer '当前选择的会计期间的索引
    Private m_periodList As List(Of String) '当前账套的科目汇总相关的会计期间集合
    Private m_acctsumList As List(Of ClsTAccountSum) '选择的期间的科目汇总数据集合

    Private Sub frmLedgerSumAccount_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        '初始化列表头部
        InitialListViewHeader()

        '加载科目汇总表月份数据
        LoadWorkPeriodData()

        If m_periodList IsNot Nothing Then
            Me.cmbWorkPeriod.DataSource = m_periodList
            'Me.cmbWorkPeriod.SelectedIndex = -1
        Else
            Me.cmbWorkPeriod.SelectedIndex = -1
        End If

    End Sub

    ''' <summary>
    ''' 从数据库中加载科目汇总相关的会计期间
    ''' </summary>
    Private Sub LoadWorkPeriodData()
        Dim tmpASDb As ClsTAccountSumDbAccess = New ClsTAccountSumDbAccess(sysUserDataConnectionString)
        Dim tmpYear As String = sysCurrentLedgerSetting.LedgerWorkYear.Substring(0, 4)
        m_periodList = tmpASDb.GetAllPeriodByYear(tmpYear)

    End Sub

    Private Sub InitialListViewHeader()

        '初始化列表标题
        Dim header1 As ColumnHeader = New ColumnHeader()
        header1.Text = "科目编码"
        header1.Width = 80
        header1.TextAlign = HorizontalAlignment.Center
        header1.Name = "hAcctCode"


        Dim header2 As ColumnHeader = New ColumnHeader()
        With header2
            .Text = "科目名称"
            .Width = 120
            .TextAlign = HorizontalAlignment.Left
            .Name = "hAcctName"

        End With

        Dim header3 As ColumnHeader = New ColumnHeader()
        With header3
            .Text = "借方金额"
            .Width = 140
            .TextAlign = HorizontalAlignment.Right
            .Name = "hDebitSum"
            '.AutoResize = True
        End With

        Dim header4 As ColumnHeader = New ColumnHeader()
        With header4
            .Text = "贷方金额"
            .Width = 140
            .TextAlign = HorizontalAlignment.Right
            .Name = "hCreditSum"
        End With


        With Me.lstAcctSum
            .FullRowSelect = True '行选择模式
            .MultiSelect = True '不允许 多行选择
            .View = View.Details '视图选项：详细模式
            '.Width = Me.FlowLayoutPanel1.Width  '宽度占满窗体
            .HeaderStyle = ColumnHeaderStyle.Nonclickable '标题不可点击
            .Columns.Add(header1)
            .Columns.Add(header2)
            .Columns.Add(header3)
            .Columns.Add(header4)
            '根据内容调整列宽
            'ListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); 
            'ListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
        End With

        '调整行高
        Dim imgList As ImageList = New ImageList()
        imgList.ImageSize = New System.Drawing.Size(10, 20) '宽 ， 高
        Me.lstAcctSum.SmallImageList = imgList

        Me.lstAcctSum.GridLines = True '有网格

        'ImageList ImageList = New ImageList();
        'ImageList.ImageSize = New System.Drawing.Size(10, 20);   //分别是宽和高
        'listView1.SmallImageList = ImageList;
    End Sub

    Private Sub cmbWorkPeriod_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbWorkPeriod.SelectedIndexChanged

        Dim tmpIndex As Integer = Me.cmbWorkPeriod.SelectedIndex
        If tmpIndex = -1 Then
            Return
        End If

        '取出指定期间的科目汇总数据
        m_periodIndex = tmpIndex
        Dim tmpPeriod As String = Me.cmbWorkPeriod.SelectedItem
        Dim tmpASDb As ClsTAccountSumDbAccess = New ClsTAccountSumDbAccess(sysUserDataConnectionString)

        m_acctsumList = tmpASDb.GetAllAccountSumInPeriod(tmpPeriod, 1)

        If m_acctsumList.Count > 0 Then
            '加载科目汇总数据到列表中
            LoadDataToListView()
        End If
    End Sub

    ''' <summary>
    ''' 将集合数据加载到列表中
    ''' </summary>
    Private Sub LoadDataToListView()

        If m_acctsumList.Count < 1 Then
            Return
        End If

        '先清除
        Me.lstAcctSum.Items.Clear()

        Dim tmpDebitSum As Decimal = 0
        Dim tmpCreditSum As Decimal = 0
        For Each itemAcctSum As ClsTAccountSum In m_acctsumList
            Dim tmpItem As ListViewItem = New ListViewItem(itemAcctSum.AcctCode)
            tmpItem.SubItems.Add(itemAcctSum.AcctName)
            tmpItem.SubItems.Add(itemAcctSum.DebitSum.ToString("#,###.00"))
            tmpItem.SubItems.Add(itemAcctSum.CreditSum.ToString("#,###.00"))

            Me.lstAcctSum.Items.Add(tmpItem)

            tmpDebitSum += itemAcctSum.DebitSum
            tmpCreditSum += itemAcctSum.CreditSum

        Next

        '加入合计行
        Dim tmpSumItem As ListViewItem = New ListViewItem("本月合计")
        tmpSumItem.Font = New Font("宋体", 10, FontStyle.Bold)

        tmpSumItem.SubItems.Add("    ")
        tmpSumItem.SubItems.Add(tmpDebitSum.ToString("#,###.00"))
        tmpSumItem.SubItems.Add(tmpCreditSum.ToString("#,###.00"))

        Me.lstAcctSum.Items.Add(tmpSumItem)
    End Sub

    '打印预览对话框
    Private m_printpreviewDlg As PrintPreviewDialog
    '科目汇总报表文档对象
    Private m_acctSumDocument As AccountSumPrintDocument
    '页面设置
    Private m_acctSumPaper As Printing.PageSettings

    ''' <summary>
    ''' 打印科目汇总报表
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
        '打印当前显示的科目汇总数据报表

        '如果没有科目汇总数据，则退出
        If m_acctsumList Is Nothing Then
            Return
        End If
        '取当前系统的打印机设置

        '准备打印数据
        m_acctSumDocument = New AccountSumPrintDocument()
        m_acctSumDocument.AcctSumList = m_acctsumList
        m_acctSumDocument.CompanyName = sysCurrentLedgerSetting.CompanyName

        '准备打印文档
        Dim printDoc As Printing.PrintDocument = m_acctSumDocument.PrintReport()
        '准备打印预览对话框
        m_printpreviewDlg = New PrintPreviewDialog()
        m_printpreviewDlg.Document = printDoc

        '定义新按钮
        '打印对话框
        Dim btnPrintDlg As ToolStripButton = New ToolStripButton()
        btnPrintDlg.Image = My.Resources.PRINT  'Image.FromFile(IO.Path.Combine(IO.Path.Combine(IO.Path.Combine(sysAppCurrentPath, "pic"), "bmp32"), "print.bmp"))
        btnPrintDlg.DisplayStyle = ToolStripItemDisplayStyle.Image
        btnPrintDlg.ToolTipText = "设置打印机并打印"
        AddHandler btnPrintDlg.Click, AddressOf OpenPrintDlg

        '打印纸张设置
        Dim btnPaperDlg As ToolStripButton = New ToolStripButton()
        btnPaperDlg.Image = My.Resources.paperSetup  'Image.FromFile(IO.Path.Combine(IO.Path.Combine(IO.Path.Combine(sysAppCurrentPath, "pic"), "bmp32"), "new.bmp"))
        btnPaperDlg.DisplayStyle = ToolStripItemDisplayStyle.Image
        btnPaperDlg.ToolTipText = "页面设置"
        AddHandler btnPaperDlg.Click, AddressOf OpenPagerSettingDlg

        Dim newToolStrip As ToolStrip = Nothing
        '查找printpreviwDialog窗体中的打印按钮
        For Each c As Control In m_printpreviewDlg.Controls
            If TypeName(c) = "ToolStrip" Then
                newToolStrip = c
                Exit For
            End If
        Next
        If newToolStrip IsNot Nothing Then
            '替换打印按钮
            newToolStrip.Items.RemoveAt(0) '打印按钮是第一个
            newToolStrip.Items.Insert(0, btnPrintDlg)

            newToolStrip.Items.Insert(1, btnPaperDlg)
        End If

        m_printpreviewDlg.ShowDialog()

    End Sub

    ''' <summary>
    ''' 页面设置对话框
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub OpenPagerSettingDlg(sender As Object, e As EventArgs)

        Dim paperDlg As PageSetupDialog = New PageSetupDialog()

        paperDlg.Document = m_printpreviewDlg.Document

        If My.Settings.VoucherPageSettings IsNot Nothing Then
            paperDlg.PageSettings = My.Settings.VoucherPageSettings
        Else
            paperDlg.PageSettings = m_printpreviewDlg.Document.DefaultPageSettings
        End If

        paperDlg.PrinterSettings = New System.Drawing.Printing.PrinterSettings

        If paperDlg.ShowDialog() = DialogResult.OK Then
            m_printpreviewDlg.Document.DefaultPageSettings = paperDlg.PageSettings
            '同时保存到配置文件中
            My.Settings.VoucherPageSettings = paperDlg.PageSettings

            m_printpreviewDlg.PrintPreviewControl.InvalidatePreview()

        End If
    End Sub

    Private Sub OpenPrintDlg(sender As Object, e As EventArgs)
        Dim pD As PrintDialog = New PrintDialog()
        pD.Document = m_printpreviewDlg.Document

        If pD.ShowDialog = DialogResult.OK Then
            m_printpreviewDlg.Document.PrinterSettings = pD.PrinterSettings
            m_printpreviewDlg.Document.Print()
            'pD.Document.Print()

        End If
    End Sub

End Class